package cn.hcbycloud.auth.front.converter;

import cn.hcbycloud.auth.bo.LoginSuccessResultBO;
import cn.hcbycloud.auth.front.dto.FrontUserPasswordLoginRequestDTO;
import cn.hcbycloud.auth.front.dto.FrontUserPasswordLoginResponseDTO;
import cn.hcbycloud.auth.front.dto.FrontUserPasswordLoginWithImgCodeRequestDTO;
import cn.hcbycloud.common.core.utils.ObjectUtils;
import cn.hcbycloud.common.imgcode.service.BaseImageCodeProcessorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author gaoh
 * @desc 认证转换类
 * @date 2022年01月28日 上午 12:00
 */
@Component
public class FrontLoginConverter {
    @Autowired
    private BaseImageCodeProcessorService imageCodeProcessorService;

    /**
     * 校验带有图片验证码的用户名密码认证
     * @param requestDTO
     */
    public void validateUserPassword(FrontUserPasswordLoginRequestDTO requestDTO) {
        // 判断是否带有图片验证码
        if(requestDTO instanceof FrontUserPasswordLoginWithImgCodeRequestDTO) {
            FrontUserPasswordLoginWithImgCodeRequestDTO withImgCodeDTO =  (FrontUserPasswordLoginWithImgCodeRequestDTO) requestDTO;
            imageCodeProcessorService.validate(withImgCodeDTO.getRandomCode(),withImgCodeDTO.getImageCode());
        }
    }

    /**
     * 将认证成功后的返回对象进行转换
     * @param loginSuccessResultBO
     * @return
     */
    public FrontUserPasswordLoginResponseDTO modelToResponse(LoginSuccessResultBO loginSuccessResultBO) {
       return ObjectUtils.cloneObject(loginSuccessResultBO,FrontUserPasswordLoginResponseDTO.class);
    }
}